# -*-coding:utf-8 -*- #
# ---------------------------------------------------------------------------
# ProjectName:   selenium_self
# FileName:      login_page.py
# Author:       test_liu
# Datetime:     2024/8/15 上午11:14
# Description: 登录
# ---------------------------------------------------------------------------
import time

from selenium import webdriver
from selenium.webdriver import Keys

from WebTest1.common.read_excel import ReadExcel
from WebTest1.common.read_ini import ReadIni
from WebTest1.common.simple import SE


class LoginPage:
    def __init__(self, web='e'):
        if web == 'e':
            self.driver = webdriver.Edge()
        else:
            self.driver = webdriver.Chrome()

        self.driver.maximize_window()
        # 访问被测系统
        self.driver.get("http://139.9.60.85:7000/")
        self.se = SE()
        # 获取截图目录
        ini = ReadIni()
        self.screen_path = ini.get_file_path("img")

    def login(self, username, password):
        """登录功能"""
        # WebDriverWait(self.driver, 10).until(
        #     EC.presence_of_element_located((By.CSS_SELECTOR, '[type="text"]'))).send_keys(username)
        # WebDriverWait(self.driver, 10).until(
        #     EC.presence_of_element_located((By.CSS_SELECTOR, '[type="password"]'))).send_keys(password)
        # codes = WebDriverWait(self.driver, 10).until(
        #     EC.presence_of_all_elements_located((By.CSS_SELECTOR, '[class="ValidCode disabled-select"]>span')))

        # self.driver.find_element(By.CSS_SELECTOR, '[type="text"]').send_keys(username)
        # self.driver.find_element(By.CSS_SELECTOR, '[type="password"]').send_keys(password)
        # codes = self.driver.find_elements(By.CSS_SELECTOR, '[class="ValidCode disabled-select"]>span')

        self.se.css_element(self.driver, '[type="text"]').send_keys(Keys.LEFT_CONTROL+"a")
        self.se.css_element(self.driver, '[type="text"]').send_keys(Keys.LEFT_CONTROL+"x")
        self.se.css_element(self.driver, '[type="text"]').send_keys(username)
        self.se.css_element(self.driver, '[type="password"]').send_keys(Keys.LEFT_CONTROL+"a")
        self.se.css_element(self.driver, '[type="password"]').send_keys(Keys.LEFT_CONTROL+"x")
        self.se.css_element(self.driver, '[type="password"]').send_keys(password)
        codes = self.se.css_elements(self.driver, '[class="ValidCode disabled-select"]>span')
        # 获取验证码
        code = ""
        for i in codes:
            code += i.text
        # print(code)
        # 输入验证码
        # WebDriverWait(self.driver, 10).until(
        #     EC.presence_of_element_located((By.CSS_SELECTOR, '[type="text"][placeholder*="验证码"]'))).send_keys(code)
        # WebDriverWait(self.driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, '[type="button"]'))).click()

        # self.driver.find_element(By.CSS_SELECTOR, '[type="text"][placeholder*="验证码"]').send_keys(code)
        # self.driver.find_element(By.CSS_SELECTOR, '[type="button"]').click()

        self.se.css_element(self.driver, '[type="text"][placeholder*="验证码"]').send_keys(Keys.LEFT_CONTROL+"a")
        self.se.css_element(self.driver, '[type="text"][placeholder*="验证码"]').send_keys(Keys.LEFT_CONTROL+"x")
        self.se.css_element(self.driver, '[type="text"][placeholder*="验证码"]').send_keys(code)
        self.se.css_click(self.driver, '[type="button"]').click()
        time.sleep(0.09)
        res = self.se.css_elements(self.driver, '[class="el-message__content"]')[-1].text
        # print(res)
        # 获取页面源码
        # self.driver.page_source,
        return res

    def save_screen(self, id):
        time.sleep(0.1)
        self.driver.get_screenshot_as_file(f"{self.screen_path}/login_{id}.png")


if __name__ == '__main__':
    login = LoginPage(web='e')
    excel = ReadExcel('login')
    dates = excel.get_data()
    for data in dates:
        res = login.login(data[0], data[1])[1]
        # 断言
        if data[-2] == res:
            excel.write_date(data[-1], 'pass')
        else:
            excel.write_date(data[-1], 'fail')
            login.save_screen(data[-2])